package com.geovis.emergency.spd.controller.pc;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPublishInfoService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillResponseInfoService;
import com.geovis.emergency.spd.biz.pc.service.IPcSchedulingEventService;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPublishInfo;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillResponseInfo;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcSchedulingEventVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/pc/drill/response/info")
@RequiredArgsConstructor
@Api(value = "应急预案指挥模块-响应行动", tags = "应急预案指挥模块-响应行动")
@Slf4j
@Validated
public class PcDrillResponseInfoController extends BaseController<IPcDrillResponseInfoService> {
    private final IPcSchedulingEventService pcSchedulingEventService;

    @ApiOperation(value = "列表", notes = "列表")
    @GetMapping("/list/{drillId}")
    public Result<List<PcDrillResponseInfo>> list(@PathVariable("drillId") String drillId) {
        return Result.ok(baseService.list(
                Wrappers.lambdaQuery(PcDrillResponseInfo.class)
                        .eq(PcDrillResponseInfo::getDrillId,drillId)
                        .orderByDesc(PcDrillResponseInfo::getCreateTime)
        ));
    }
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping("save")
    public Result<List<PcDrillResponseInfo>> save(@RequestBody List<PcDrillResponseInfo> list) {
        PcSchedulingEventVO detailEvent = null;
        for (PcDrillResponseInfo pcDrillResponseInfo : list) {
            if (ObjectUtil.isNull(detailEvent)){
                detailEvent = pcSchedulingEventService.getDetailEventByDrillId(list.get(0).getDrillId());
            }
            if (ObjectUtil.isNotNull(detailEvent)){
                pcDrillResponseInfo.setEventId(detailEvent.getId());
            }
        }
        baseService.saveOrUpdateBatch(list);
        return Result.ok();
    }
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "批量删除", notes = "批量删除数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeBatchByIds(idList);
        return Result.ok();
    }
}
